#-*- coding: Utf-8 -*-
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django import forms
from django.conf import settings
from django.core.urlresolvers import reverse
import os

class AdminFileWidget(forms.FileInput):
    def render(self, name, value, attrs=None):
        output = []
        if value and hasattr(value, 'url'):
            output.append(render_to_string('pagemagic/file_widget.html', {'value': value }))
        output.append(super(AdminFileWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))


class AdminImageWidget(forms.FileInput):
    def render(self, name, value, attrs=None):
        output = []
        if value and hasattr(value, 'url'):
            output.append(render_to_string('pagemagic/image_widget.html', {'value': value }))
        output.append(super(AdminImageWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))


class VisualEditorWidget(forms.Textarea):
    def __init__(self, attrs=None):
        if not attrs:
            attrs = {}
        attrs['class'] = 'visual_editor'
        return super(VisualEditorWidget, self).__init__(attrs)

    class Media:
        js = getattr(settings, 'VISUAL_EDITOR_JS', ())
        css = {'all': getattr(settings, 'VISUAL_EDITOR_CSS', ())}


class LargeTextInput(forms.TextInput):
    def render(self, name, value, attrs=None):
        if not attrs:
            attrs = {}
        attrs['style'] = 'width:40em'  # TODO make better
        return super(LargeTextInput, self).render(name, value, attrs)


class PreviewableTextarea(forms.Textarea):
    def __init__(self, *args, **kwargs):
        self.autoformat_checkbox_id = kwargs.pop('autoformat_checkbox_id', None)
        return super(PreviewableTextarea, self).__init__(*args, **kwargs)

    def render(self, name, value, attrs=None):
        output = []
        output.append(super(PreviewableTextarea, self).render(name, value, attrs))
        output.append(render_to_string('pagemagic/previewable_textarea.html', {
            'value': value,
            'attrs': attrs,
            'autoformat_checkbox_id': self.autoformat_checkbox_id,
            'preview_url': reverse('text_preview'),
        }))
        return mark_safe(u''.join(output))


class SimpleVisualEditor(forms.Textarea):
    def render(self, name, value, attrs=None):
        output = []
        output.append(super(SimpleVisualEditor, self).render(name, value, attrs))
        output.append('''<script>init_simple_visual_editor({
            textarea: document.getElementById("%s"),
            css: %s,
            debug_css: %s
        })</script>''' % (
            attrs['id'],
            repr([os.path.join(settings.MEDIA_URL, x) for x in getattr(settings, 'SIMPLE_VISUAL_EDITOR_CSS', [])]),
            repr([os.path.join(settings.MEDIA_URL, x) for x in getattr(settings, 'SIMPLE_VISUAL_EDITOR_DEBUG_CSS', [
                'simple_visual_editor/debug.css',
            ])])
        ))
#        output.append('<script type="text/javascript" src="%s"></script>' %
#            reverse('django.views.i18n.javascript_catalog'))
        return mark_safe(u''.join(output))

    class Media:
        css = {
            'all': (
                'simple_visual_editor/editor.css',
            )
        }
        js = (
            'simple_visual_editor/tabs.js',
            'simple_visual_editor/editor.js',
        )
#        js = property(lambda: (
#            reverse('django.views.i18n.javascript_catalog'),
#            'simple_visual_editor.js',
#        ))
